package cn.schoolwow.util.domain.query.resource;

import cn.schoolwow.quickflow.QuickFlow;
import cn.schoolwow.quickflow.QuickFlowBuilder;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.util.module.query.resource.domain.ResourceQueryOption;
import cn.schoolwow.util.module.query.resource.flow.*;
import cn.schoolwow.util.module.query.resource.flow.walkResource.CheckWalkResourceRequestFlow;
import cn.schoolwow.util.module.query.resource.flow.walkResource.WalkResourceFlow;
import cn.schoolwow.util.module.query.resource.service.GetResourceAsURLFlow;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.List;

public class QueryResourceResponse {
    private static QuickFlow resourceQuickFlow = QuickFlowBuilder.newInstance();

    public QueryResourceResponse(ResourceQueryOption resourceQueryOption) {
        this.resourceQuickFlow.putContextThreadLocalData("resourceQueryOption", resourceQueryOption);
    }

    /**返回资源路径*/
    public String getPath(){
        ResourceQueryOption resourceQueryOption = (ResourceQueryOption) resourceQuickFlow.getContextThreadLocalData("resourceQueryOption");
        return resourceQueryOption.path;
    }

    /**返回资源文件名称*/
    public String getFileName(){
        FlowContext flowContext = resourceQuickFlow.startFlow(new GetResourceAsURLFlow())
                .next(new GetFileNameFlow())
                .execute();
        String fileName = (String) flowContext.checkData("fileName");
        return fileName;
    }

    /**返回URL对象*/
    public URL getAsURL(){
        FlowContext flowContext = resourceQuickFlow.executeFlow(new GetResourceAsURLFlow());
        URL url = (URL) flowContext.getData("url");
        return url;
    }

    /**返回File对象*/
    public File getAsFile(){
        FlowContext flowContext = resourceQuickFlow.executeFlow(new GetResourceAsURLFlow());
        File file = (File) flowContext.checkData("file");
        return file;
    }

    /**返回字符串对象*/
    public String getAsString(){
        FlowContext flowContext = resourceQuickFlow.startFlow(new GetResourceAsURLFlow())
                .next(new GetAsStringFlow())
                .execute();
        String content = (String) flowContext.getData("content");
        return content;
    }

    /**返回JSONObject对象*/
    public JSONObject getAsJSONObject(){
        FlowContext flowContext = resourceQuickFlow.startFlow(new GetResourceAsURLFlow())
                .next(new GetAsStringFlow())
                .next(new ParseStringAsJSONObjectFlow())
                .execute();
        JSONObject jsonObject = (JSONObject) flowContext.getData("jsonObject");
        return jsonObject;
    }

    /**返回JSONArray对象*/
    public JSONArray getAsJSONArray(){
        FlowContext flowContext = resourceQuickFlow.startFlow(new GetResourceAsURLFlow())
                .next(new GetAsStringFlow())
                .next(new ParseStringAsJSONArrayFlow())
                .execute();
        JSONArray jsonArray = (JSONArray) flowContext.getData("jsonArray");
        return jsonArray;
    }

    /**返回实例*/
    public <T> T getAsInstance(Class<T> clazz){
        FlowContext flowContext = resourceQuickFlow.startFlow(new GetResourceAsURLFlow())
                .next(new GetAsStringFlow())
                .next(new ParseStringAsJSONObjectFlow())
                .next(new JSONObjectToInstanceFlow<T>())
                .putCurrentCompositeFlowData("clazz", clazz)
                .execute();
        T instance = (T) flowContext.getData("instance");
        return instance;
    }

    /**返回实例列表*/
    public <T> List<T> getAsInstanceList(Class<T> clazz){
        FlowContext flowContext = resourceQuickFlow.startFlow(new GetResourceAsURLFlow())
                .next(new GetAsStringFlow())
                .next(new ParseStringAsJSONArrayFlow())
                .next(new JSONArrayToInstanceListFlow())
                .putCurrentCompositeFlowData("clazz", clazz)
                .execute();
        List<T> instanceList = (List<T>) flowContext.getData("instanceList");
        return instanceList;
    }

    /**返回输入流*/
    public InputStream getAsInputStream(){
        FlowContext flowContext = resourceQuickFlow.startFlow(new GetResourceAsURLFlow())
                .next(new OpenUrlInputStreamFlow())
                .execute();
        InputStream inputStream = (InputStream) flowContext.getData("inputStream");
        return inputStream;
    }

    /**复制到指定文件*/
    public QueryResourceResponse copyToFile(String targetFilePath){
        resourceQuickFlow.startFlow(new GetResourceAsURLFlow())
                .next(new CopyToFileFlow())
                .putCurrentCompositeFlowData("targetFilePath", targetFilePath)
                .execute();
        return this;
    }

    /**循环处理单个文件*/
    public QueryResourceResponse walkResource(){
        resourceQuickFlow.startFlow(new GetResourceAsURLFlow())
                .next(new CheckWalkResourceRequestFlow())
                .next(new WalkResourceFlow())
                .execute();
        return this;
    }
}
